const app = getApp();
const ipconfig = app.globalData.ipconfig;
Component({

    properties:{
        placeholder: String
    },
  data: {
    formats: {},
    readOnly: false,
    placeholder: '开始输入...',
    editorHeight: 300,
    keyboardHeight: 0,
    isIOS: false,
    imgUrl:''
  },
  methods:{
      readOnlyChange() {
        this.setData({
          readOnly: !this.data.readOnly
        })
      },
      onLoad(e) {
        const platform = wx.getSystemInfoSync().platform
        const isIOS = platform === 'ios'
        this.setData({ isIOS})
        const that = this
        this.updatePosition(0)
        let keyboardHeight = 0
        wx.onKeyboardHeightChange(res => {
          if (res.height === keyboardHeight) return
          const duration = res.height > 0 ? res.duration * 1000 : 0
          keyboardHeight = res.height
          setTimeout(() => {
            wx.pageScrollTo({
              scrollTop: 0,
              success() {
                that.updatePosition(keyboardHeight)
                that.editorCtx.scrollIntoView()
              }
            })
          }, duration)
    
        })
      },
      updatePosition(keyboardHeight) {
        const toolbarHeight = 50
        const { windowHeight, platform } = wx.getSystemInfoSync()
        let editorHeight = keyboardHeight > 0 ? (windowHeight - keyboardHeight - toolbarHeight) : windowHeight
        this.setData({ editorHeight, keyboardHeight })
      },
      calNavigationBarAndStatusBar() {
        const systemInfo = wx.getSystemInfoSync()
        const { statusBarHeight, platform } = systemInfo
        const isIOS = platform === 'ios'
        const navigationBarHeight = isIOS ? 44 : 48
        return statusBarHeight + navigationBarHeight
      },
      /**编译器内容改变时触发 */
      onBindInput(e) {
        this.triggerEvent('input', e.detail.html)
        // console.log("blur----------->", e)
        console.log(e.detail);
       
      },
      /**编辑器失去焦点时触发 */
      onBindBlur(e) {
        this.triggerEvent('blur', e.detail)
        // console.log("blur----------->", e)
      },
      onEditorReady() {
        const that = this
        // wx.createSelectorQuery().select('#editor').context(function (res) {
        //   that.editorCtx = res.context
		// }).exec()
		
		// 注意这个地方，加了.in(this),因为在自定义组件中应使用this.createSelectorQuery() 来代替 wx.createSelectorQuery()
		//https://developers.weixin.qq.com/miniprogram/dev/api/wxml/wx.createSelectorQuery.html
		wx.createSelectorQuery().in(this).select('#editor').context(function (res) {
			that.editorCtx = res.context
			// 通知主页面，不懂可去官方文档查看
			//https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/events.html#%E8%A7%A6%E5%8F%91%E4%BA%8B%E4%BB%B6
			that.triggerEvent('ready', that)
    	  }).exec()	
      },
      /**初始化编辑器内容，html和delta同时存在时仅delta生效 */
    setContents(val) {
        this.editorCtx.setContents({
          html: val,
          success: function () {
            console.log('setContents success')
          }
        })
      },
      blur() {
        this.editorCtx.blur()
      },
      format(e) {
        let { name, value } = e.target.dataset
        if (!name) return
        // console.log('format', name, value)
        this.editorCtx.format(name, value)
    
      },
      onStatusChange(e) {
        const formats = e.detail
        this.setData({ formats })
      },
      insertDivider() {
        this.editorCtx.insertDivider({
          success: function () {
            console.log('insert divider success')
          }
        })
      },
      clear() {
        this.editorCtx.clear({
          success: function (res) {
            console.log("clear success")
          }
        })
      },
      removeFormat() {
        this.editorCtx.removeFormat()
      },
      insertDate() {
        const date = new Date()
        const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
        this.editorCtx.insertText({
          text: formatDate
        })
      },
      insertImage() {
        const that = this
        wx.chooseImage({
          count: 1,
          success: function (res) {
              const tempFilePath = res.tempFilePaths[0];
            wx.uploadFile({
                url: 'http://' + ipconfig + ':8080/tool/upload',
                filePath: tempFilePath,
                name: 'file',
                header: {
                    'Authorization': wx.getStorageSync('token')
                },
                success: function (res) {
                    const data = JSON.parse(res.data);
					that.editorCtx.insertImage({
						src: "http://rrjnomlap.hn-bkt.clouddn.com/"+data.data,
						data: {
						  id: 'imgage',
						  role: 'god'
						},
						width: '80%',
						success: function () {
						  console.log('insert image success')
						}
					  })
                    
                },
                fail: function (err) {
                    console.log(err);
                }
			});
			
             

          }
        })
      },
      uploadFile: function (e) {
        const that = this;
        wx.chooseMedia({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            mediaType: ['image', 'video'],
            maxDuration: 30,
            camera: 'back',
            success: function (res) {
                var tempFilePath = res.tempFiles[0].tempFilePath;
            },
            fail: function (err) {
                console.log(err);
            }
        })
    },
  }
 
})
